<template>
  <div class="login-container">
    <vue-particles
      :particle-opacity="0.7"
      :particles-number="80"
      :particle-size="4"
      :lines-width="1"
      :line-linked="true"
      :line-opacity="0.4"
      :lines-distance="150"
      :move-speed="3"
      :hover-effect="true"
      :click-effect="true"
      color="#FFFFFF"
      shape-type="circle"
      lines-color="#FFFFFF"
      hover-mode="grab"
      click-mode="push"
      class="particles"
    />
    <el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form" auto-complete="off" label-position="left">
      <div class="title-container">
        <h3 class="title"><img src="@/assets/logo.png" class="sidebar-logo">{{ $t('login.title') }}</h3>
      </div>
      <el-form-item prop="username">
        <span class="svg-container">
          <svg-icon icon-class="user" />
        </span>
        <el-input
          v-model="loginForm.username"
          :placeholder="$t('login.username')"
          name="username"
          type="text"
          auto-complete="off"
          @keyup.enter.native="handleLogin"
        />
      </el-form-item>

      <el-form-item prop="password">
        <span class="svg-container">
          <svg-icon icon-class="password" />
        </span>
        <el-input
          :type="passwordType"
          v-model="loginForm.password"
          :placeholder="$t('login.password')"
          name="password"
          auto-complete="off"
          @keyup.enter.native="handleLogin" />
        <span class="show-pwd" @click="showPwd">
          <svg-icon :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'" />
        </span>
      </el-form-item>

      <div class="verify">
        <el-form-item prop="verify">
          <el-input
            v-model="loginForm.verify"
            :placeholder="$t('login.verifyPlaceholder')"
            name="verify"
            type="text"
            auto-complete="off"
            maxlength="5"
            @keyup.enter.native="handleLogin"
          />
        </el-form-item>
      </div>
      <div class="imgVerify" @click="changeVerify(true)">
        <img :src="verifySrc" :alt="$t('login.changeVerify')" :title="$t('login.changeVerify')" style="height:49px; width: 100%; cursor: pointer;" >
      </div>

      <el-button :loading="loading" type="primary" style="width:100%;margin-bottom:30px;" @keyup.enter="handleLogin" @click.native.prevent="handleLogin">{{ $t('login.logIn') }}</el-button>
    </el-form>

  </div>
</template>

<script>
export default {
  name: 'Login',
  data() {
    const validateUsername = (rule, value, callback) => {
      if (!value) {
        callback(new Error('请输入账号'))
      } else {
        callback()
      }
    }
    const validatePassword = (rule, value, callback) => {
      if (value.length < 5) {
        callback(new Error('密码不能少于5位'))
      } else {
        callback()
      }
    }
    const validateVerify = (rule, value, callback) => {
      if (value.length !== 5) {
        callback(new Error('验证码不正确'))
      } else {
        callback()
      }
    }
    return {
      loginForm: {
        username: '',
        password: '',
        verify: ''
      },
      loginRules: {
        username: [{ required: true, trigger: 'blur', validator: validateUsername }],
        password: [{ required: true, trigger: 'blur', validator: validatePassword }],
        verify: [{ required: true, trigger: 'blur', validator: validateVerify }]
      },
      passwordType: 'password',
      loading: false,
      showDialog: false,
      redirect: undefined,
      verifySrc: '/login/verify'
    }
  },
  watch: {
    $route: {
      handler: function(route) {
        this.redirect = route.query && route.query.redirect
      },
      immediate: true
    }
  },
  created() {

  },
  destroyed() {

  },
  methods: {
    showPwd() {
      this.passwordType = this.passwordType === 'password' ? '' : 'password'
    },
    changeVerify: function() {
      this.verifySrc = '/login/verify?' + Math.random()
    },
    handleLogin() {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.loading = true
          this.$store.dispatch('LoginByUsername', this.loginForm).then(() => {
            this.$router.push({ path: this.redirect || '/' })
          }).catch((error) => {
            if (error === '验证码错误') {
              this.changeVerify()
              this.loginForm.verify = ''
            }
            this.loading = false
          })
        } else {
          // console.log('请输入正确的账号和密码')
          return false
        }
      })
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
  $bg:#283443;
  $light_gray:#eee;
  $cursor: #fff;

  @supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
    .login-container .el-input input{
      color: $cursor;
      &::first-line {
        color: $light_gray;
      }
    }
  }

  /* reset element-ui css */
  .login-container {
    .particles{
      position:fixed;
      top:0;
      width:100%;
    }
    .login-form{
      background-color: $bg;
      .sidebar-logo {
        height: 32px;
        vertical-align: middle;
        margin-right: 12px;
      }
    }
    /deep/ .el-input {
      display: inline-block;
      height: 47px;
      width: 85%;
      input {
        background: transparent;
        border: 0;
        -webkit-appearance: none;
        border-radius: 0;
        padding: 5px 5px 5px 15px;
        color: $light_gray;
        height: 42px;
        caret-color: $cursor;
        &:-webkit-autofill {
          -webkit-box-shadow: 0 0 0 1000px #242f3c inset !important;
          -webkit-text-fill-color: $cursor !important;
        }
      }
    }
    /deep/ .el-form-item {
      border: 1px solid rgba(255, 255, 255, 0.1);
      background: rgba(0, 0, 0, 0.1);
      border-radius: 5px;
      color: #454545;
    }
    .verify{
      width: 50%;
      display: inline-block;
      .el-input {
        width: 100%;
      }
    }
    .imgVerify{
      width: 45%;
      float: right;
      height: 47px;
    }
  }
$bg:#2d3a4b;
$dark_gray:#889aa4;
$light_gray:#eee;

.login-container {
  position: fixed;
  height: 100%;
  width: 100%;
  background-color: $bg;
  .login-form {
    position: absolute;
    left: 0;
    right: 0;
    width: 520px;
    max-width: 100%;
    padding: 35px 35px 15px 35px;
    margin: 120px auto;
  }
  .tips {
    font-size: 14px;
    color: #fff;
    margin-bottom: 10px;
    span {
      &:first-of-type {
        margin-right: 16px;
      }
    }
  }
  .svg-container {
    padding: 6px 5px 6px 15px;
    color: $dark_gray;
    vertical-align: middle;
    width: 30px;
    display: inline-block;
  }
  .title-container {
    position: relative;
    .title {
      font-size: 26px;
      color: $light_gray;
      margin: 0 auto 40px auto;
      text-align: center;
      font-weight: bold;
    }
    .set-language {
      color: #fff;
      position: absolute;
      top: 5px;
      right: 0;
    }
  }
  .show-pwd {
    position: absolute;
    right: 10px;
    top: 7px;
    font-size: 16px;
    color: $dark_gray;
    cursor: pointer;
    user-select: none;
  }
  .thirdparty-button {
    position: absolute;
    right: 35px;
    bottom: 28px;
  }
}
</style>
